package gui;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.RowFilter;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXList;

import sys.BrowseSourceTableModel;
import sys.Video;
import sys.Videos;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JTextField;
import loc.messages;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import org.jdesktop.swingx.JXTable;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class BrowseSources extends JDialog implements KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7755250953731983038L;
	private final JPanel contentPanel = new JPanel();
	private JLabel lblStatus;
	private final Action actionCancel = new SwingAction();
	private JTextField textField;
	private final Action actionOk = new SwingAction_1();
	private final Action actionUpdate = new SwingAction_2();
	private Videos availableVideos;
	private JButton btnUpdate;
	private JXTable table;
	
	/**
	 * Create the dialog.
	 */
	public BrowseSources() {
		setTitle(messages.getString("BrowseSources.0")); //$NON-NLS-1$
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
		setBounds(100, 100, 800, 600);
		setLocationRelativeTo(null); 
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(UIManager.getColor("window")); //$NON-NLS-1$
		contentPanel.setBorder(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[grow][grow][grow][grow,fill]", "[][fill][grow,fill]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		
		textField = new JTextField();
		textField.addKeyListener(this);
		
		JLabel lblFilter = new JLabel(messages.getString("BrowseSources.lblFilter.text")); //$NON-NLS-1$
		contentPanel.add(lblFilter, "cell 0 0 4 1");
		contentPanel.add(textField, "cell 0 1 4 1,growx"); //$NON-NLS-1$
		textField.setColumns(10);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		contentPanel.add(scrollPane_1, "cell 0 2 4 1,grow");
		
		table = new JXTable();
		table.setEditable(false);
		table.setShowVerticalLines(false);
		table.setModel(new BrowseSourceTableModel());
		table.getTableHeader().getColumnModel().getColumn(0).setHeaderValue("Game"); //$NON-NLS-1$
		table.getTableHeader().getColumnModel().getColumn(1).setHeaderValue("Producer"); //$NON-NLS-1$
		table.getTableHeader().getColumnModel().getColumn(2).setHeaderValue("Source"); //$NON-NLS-1$
		table.getTableHeader().getColumnModel().getColumn(3).setHeaderValue("Title"); //$NON-NLS-1$
		scrollPane_1.setViewportView(table);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(160, 160, 160))); //$NON-NLS-1$
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		buttonPane.setLayout(new MigLayout("", "[left][grow,fill][right][right][right]", "[]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		
		lblStatus = new JLabel(messages.getString("BrowseSources.11")); //$NON-NLS-1$
		buttonPane.add(lblStatus, "cell 0 0,alignx left,growy"); //$NON-NLS-1$
		
		btnUpdate = new JButton(messages.getString("BrowseSources.btnUpdate.text")); //$NON-NLS-1$
		btnUpdate.setAction(actionUpdate);
		buttonPane.add(btnUpdate, "cell 2 0,alignx right,growy");
	
		JButton okButton = new JButton(messages.getString("BrowseSources.13")); //$NON-NLS-1$
		okButton.setAction(actionOk);
		okButton.setActionCommand(messages.getString("BrowseSources.14")); //$NON-NLS-1$
		buttonPane.add(okButton, "cell 3 0,alignx right,growy"); //$NON-NLS-1$
		getRootPane().setDefaultButton(okButton);
		
		JButton cancelButton = new JButton(messages.getString("BrowseSources.16")); //$NON-NLS-1$
		cancelButton.setAction(actionCancel);
		cancelButton.setActionCommand(messages.getString("BrowseSources.17")); //$NON-NLS-1$
		buttonPane.add(cancelButton, "cell 4 0,alignx right,growy"); //$NON-NLS-1$
		
		loadAndSetModel();
		
	}
	
	private void loadAndSetModel() {
		new Thread() {
			public void run() {
				btnUpdate.setEnabled(false);
				table.setEnabled(false);
				table.clearSelection();
				lblStatus.setText(messages.getString("BrowseSources.11"));
				
				availableVideos = new Videos();
								
				setModel();
				lblStatus.setText(messages.getString("BrowseSources.19")); //$NON-NLS-1$
				table.setEnabled(true);
				btnUpdate.setEnabled(true);
				checkVideos();
			}
		}.start();
	}

	private void setModel() {
		((BrowseSourceTableModel)table.getModel()).replaceVideosWith(availableVideos.getVideos());
	}
	
	public void checkVideos() {
		//TODO Implement a feature that disable videos already chosen.
	}
	
	private class SwingAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -2062278424283161905L;
		public SwingAction() {
			putValue(NAME, messages.getString("BrowseSources.21")); //$NON-NLS-1$
			putValue(SHORT_DESCRIPTION, messages.getString("BrowseSources.22")); //$NON-NLS-1$
		}
		public void actionPerformed(ActionEvent e) {
			setVisible(false);
		}
	}
	
	private void setVideoFilter(String filter) {
		table.setRowFilter(RowFilter.regexFilter("(?i)" + filter, 0,1,2,3));
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getSource().getClass().getName().contains("JTextField")) { //$NON-NLS-1$
			this.setVideoFilter(((JTextField)e.getSource()).getText());
		}
	}
	
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	private class SwingAction_1 extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -1438091962785366653L;
		public SwingAction_1() {
			putValue(NAME, messages.getString("BrowseSources.26")); //$NON-NLS-1$
			putValue(SHORT_DESCRIPTION, messages.getString("BrowseSources.27")); //$NON-NLS-1$
		}
		public void actionPerformed(ActionEvent e) {
			ArrayList<Video> videos = new ArrayList<Video>();
			for(int i : table.getSelectedRows()) {
				videos.add(((BrowseSourceTableModel) table.getModel()).getVideo(i));
			}
			JPanelMainContent.getVideoList().addVideos(videos);
			table.clearSelection();
			setVisible(false);
		}
	}
	private class SwingAction_2 extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		public SwingAction_2() {
			putValue(NAME, "Update");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			loadAndSetModel();
		}
	}
}
